package com.bysj.sausys.controller.backend;

import com.bysj.sausys.beans.PageQuery;
import com.bysj.sausys.beans.PageResult;
import com.bysj.sausys.common.CadreConst;
import com.bysj.sausys.common.JsonData;
import com.bysj.sausys.common.UserConst;
import com.bysj.sausys.model.SysSauCadre;
import com.bysj.sausys.service.ISysSauCadreService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 社联干部Controller
 *
 * @author WangZK
 */
@Api(description = "社联干部信息接口")
@Controller
@RequestMapping("/sys/saucadre")
public class SysSauCadreController {

    @Resource
    private ISysSauCadreService sysSauCadreService;

    /**
     * 添加社联干部
     *
     * @param sysSauCadre
     * @param request
     * @return
     */
    @ApiOperation(value = "添加社联干部信息接口")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public JsonData save(@ApiParam(value = "社联干部实体类", required = true) @RequestBody SysSauCadre sysSauCadre,
                         HttpServletRequest request) {
        // 从session中获取当前用户
        Map map = UserConst.getCurrentUser(request);
        sysSauCadreService.save(sysSauCadre, UserConst.getCurrentUsername(map));
        return JsonData.success();
    }

    /**
     * 更新社联干部
     *
     * @param sysSauCadre
     * @param request
     * @return
     */
    @ApiOperation(value = "更新社联干部信息接口")
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    @ResponseBody
    public JsonData update(@ApiParam(value = "社联干部实体类", required = true) @RequestBody SysSauCadre sysSauCadre,
                           HttpServletRequest request) {
        // 从session中获取当前用户
        Map map = UserConst.getCurrentUser(request);
        sysSauCadreService.update(sysSauCadre, UserConst.getCurrentUsername(map));
        return JsonData.success();
    }

    /**
     * 分页获取社联干部列表
     *
     * @param keyword
     * @param pageNo
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "分页获取社联干部列表接口")
    @RequestMapping(value = "/getPage", method = RequestMethod.GET)
    @ResponseBody
    public JsonData getPage(@ApiParam(value = "关键字") @RequestParam(required = false) String keyword,
                            @ApiParam(value = "页码") @RequestParam(required = false, defaultValue = "1") int pageNo,
                            @ApiParam(value = "每页显示条数") @RequestParam(required = false, defaultValue = "10") int pageSize) {
        PageResult result = sysSauCadreService.getPageByKeyword(keyword, new PageQuery(pageNo, pageSize));
        return JsonData.success(result);
    }

    /**
     * 根据id获取社联干部信息
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id获取社联干部信息接口")
    @RequestMapping(value = "/getOne", method = RequestMethod.GET)
    @ResponseBody
    public JsonData getOne(@ApiParam(value = "社联干部id", required = true) @RequestParam Integer id) {
        SysSauCadre sysSauCadre = sysSauCadreService.findById(id);
        return JsonData.success(sysSauCadre);
    }

    /**
     * 修改社联干部的状态为在职
     *
     * @param ids
     * @param request
     * @return
     */
    @ApiOperation(value = "修改社联干部的状态为在职接口")
    @RequestMapping(value = "/job", method = RequestMethod.PUT)
    @ResponseBody
    public JsonData job(@ApiParam(value = "社联干部id数组", required = true) @RequestParam Integer[] ids,
                        HttpServletRequest request) {
        // 从session中获取当前用户
        Map map = UserConst.getCurrentUser(request);
        sysSauCadreService.updateStatus(ids, CadreConst.JOB.getCode(), UserConst.getCurrentUsername(map));
        return JsonData.success();
    }

    /**
     * 修改社联干部的状态为离职
     *
     * @param ids
     * @param request
     * @return
     */
    @ApiOperation(value = "修改社联干部的状态为离职接口")
    @RequestMapping(value = "/quit", method = RequestMethod.PUT)
    @ResponseBody
    public JsonData quit(@ApiParam(value = "社联干部id数组", required = true) @RequestParam Integer[] ids,
                         HttpServletRequest request) {
        // 从session中获取当前用户
        Map map = UserConst.getCurrentUser(request);
        sysSauCadreService.updateStatus(ids, CadreConst.QUIT.getCode(), UserConst.getCurrentUsername(map));
        return JsonData.success();
    }

    /**
     * 删除社联干部
     *
     * @param ids
     * @param request
     * @return
     */
    @ApiOperation(value = "删除社联干部接口")
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    @ResponseBody
    public JsonData delete(@ApiParam(value = "社联干部id数组", required = true) @RequestParam Integer[] ids,
                           HttpServletRequest request) {
        sysSauCadreService.delete(ids);
        return JsonData.success();
    }

}
